import { expect, it, describe } from 'vitest';
import { Field } from '@directus/types';
import { getFieldsInGroup } from './get-fields-in-group.js';

describe('getFieldsInGroup', () => {
	const fields: Field[] = [
		{
			collection: 'article',
			field: 'root_group',
			type: 'alias',
			schema: null,
			meta: {
				id: 11,
				collection: 'article',
				field: 'root_group',
				special: ['alias', 'no-data', 'group'],
				interface: 'group-detail',
				options: null,
				display: null,
				display_options: null,
				readonly: false,
				hidden: false,
				sort: 7,
				width: 'full',
				translations: null,
				note: null,
				conditions: [
					{
						name: 'subtitle_conditions',
						rule: {
							_and: [
								{
									clear_group_fields: {
										_eq: true,
									},
								},
							],
						},
						hidden: true,
						clear_hidden_value_on_save: true,
					},
				],
				required: false,
				group: null,
				validation: null,
				validation_message: null,
			},
			name: 'Root Group',
		},
		{
			collection: 'article',
			field: 'group_title',
			type: 'string',
			schema: {
				name: 'group_title',
				table: 'article',
				data_type: 'varchar',
				default_value: null,
				max_length: 255,
				numeric_precision: null,
				numeric_scale: null,
				is_generated: false,
				generation_expression: null,
				is_nullable: true,
				is_unique: false,
				is_indexed: false,
				is_primary_key: false,
				has_auto_increment: false,
				foreign_key_column: null,
				foreign_key_table: null,
			},
			meta: {
				id: 7,
				collection: 'article',
				field: 'group_title',
				special: null,
				interface: 'input',
				options: null,
				display: null,
				display_options: null,
				readonly: false,
				hidden: false,
				sort: 1,
				width: 'full',
				translations: null,
				note: null,
				conditions: null,
				required: false,
				group: 'root_group',
				validation: null,
				validation_message: null,
			},
			name: 'Group Title',
		},
		{
			collection: 'article',
			field: 'group_subtitle',
			type: 'string',
			schema: {
				name: 'group_subtitle',
				table: 'article',
				data_type: 'varchar',
				default_value: null,
				max_length: 255,
				numeric_precision: null,
				numeric_scale: null,
				is_generated: false,
				generation_expression: null,
				is_nullable: true,
				is_unique: false,
				is_indexed: false,
				is_primary_key: false,
				has_auto_increment: false,
				foreign_key_column: null,
				foreign_key_table: null,
			},
			meta: {
				id: 8,
				collection: 'article',
				field: 'group_subtitle',
				special: null,
				interface: 'input',
				options: null,
				display: null,
				display_options: null,
				readonly: false,
				hidden: false,
				sort: 2,
				width: 'full',
				translations: null,
				note: null,
				conditions: null,
				required: false,
				group: 'root_group',
				validation: null,
				validation_message: null,
			},
			name: 'Group Subtitle',
		},
		{
			collection: 'article',
			field: 'subgroup',
			type: 'alias',
			schema: null,
			meta: {
				id: 5,
				collection: 'article',
				field: 'subgroup',
				special: ['alias', 'no-data', 'group'],
				interface: 'group-detail',
				options: null,
				display: null,
				display_options: null,
				readonly: false,
				hidden: false,
				sort: 4,
				width: 'full',
				translations: null,
				note: null,
				conditions: null,
				required: false,
				group: 'root_group',
				validation: null,
				validation_message: null,
			},
			name: 'Subgroup',
		},
		{
			collection: 'article',
			field: 'group2_title',
			type: 'string',
			schema: {
				name: 'group2_title',
				table: 'article',
				data_type: 'varchar',
				default_value: null,
				max_length: 255,
				numeric_precision: null,
				numeric_scale: null,
				is_generated: false,
				generation_expression: null,
				is_nullable: true,
				is_unique: false,
				is_indexed: false,
				is_primary_key: false,
				has_auto_increment: false,
				foreign_key_column: null,
				foreign_key_table: null,
			},
			meta: {
				id: 12,
				collection: 'article',
				field: 'group2_title',
				special: null,
				interface: 'input',
				options: null,
				display: null,
				display_options: null,
				readonly: false,
				hidden: false,
				sort: 1,
				width: 'full',
				translations: null,
				note: null,
				conditions: null,
				required: false,
				group: 'subgroup',
				validation: null,
				validation_message: null,
			},
			name: 'Group2 Title',
		},
		{
			collection: 'article',
			field: 'group2_subtitle',
			type: 'string',
			schema: {
				name: 'group2_subtitle',
				table: 'article',
				data_type: 'varchar',
				default_value: null,
				max_length: 255,
				numeric_precision: null,
				numeric_scale: null,
				is_generated: false,
				generation_expression: null,
				is_nullable: true,
				is_unique: false,
				is_indexed: false,
				is_primary_key: false,
				has_auto_increment: false,
				foreign_key_column: null,
				foreign_key_table: null,
			},
			meta: {
				id: 13,
				collection: 'article',
				field: 'group2_subtitle',
				special: null,
				interface: 'input',
				options: null,
				display: null,
				display_options: null,
				readonly: false,
				hidden: false,
				sort: 2,
				width: 'full',
				translations: null,
				note: null,
				conditions: null,
				required: false,
				group: 'subgroup',
				validation: null,
				validation_message: null,
			},
			name: 'Group2 Subtitle',
		},
	];

	it('should return fields in the given group, including nested groups and fields within the nested groups', () => {
		const result = getFieldsInGroup('root_group', fields);
		expect(result).toHaveLength(5);
		expect(result.map((f) => f.field)).toContain('group_title');
		expect(result.map((f) => f.field)).toContain('group_subtitle');
		expect(result.map((f) => f.field)).toContain('subgroup');
		expect(result.map((f) => f.field)).toContain('group2_title');
		expect(result.map((f) => f.field)).toContain('group2_subtitle');
	});

	it('should return empty array for non-existent group', () => {
		const result = getFieldsInGroup('non_existent', fields);
		expect(result).toHaveLength(0);
	});
});
